<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { equipApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    status:0
  })
  interface formQuert {
    status:number
  }
  const form = reactive({} as formQuert)
  const handleSubmit = ()=>{
    const { status } = form
    ElMessageBox.confirm(`确认${status==1?'开启':'关闭'}幸运装备星游戏吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
    .then(async () => {
      const loading = Toastloading();
      await equipApi.gameStatus(form);
      loading.close();
      ToastSuccess("操作成功!");
      $emit('updateList')
      closeDialog()
    })
    .catch(() => {
      form.status = action.status
    });
  }
  const openDialog = (query:any = {})=> {
    const { status, game_id, game_name } = query
    action.title = game_name + '游戏状态修改'
    action.status = status
    Object.assign(form,{status,game_id})
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    Object.assign(form,{
      game_id:0
    })
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="30%"
    :title="action.title">
      <el-radio-group v-model="form.status">
        <el-radio-button :label="1">开启游戏</el-radio-button>
        <el-radio-button :label="0">关闭游戏</el-radio-button>
      </el-radio-group>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>